package cn.kgc.controller;

import cn.kgc.pojo.*;
import cn.kgc.service.Service_gzk;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
//////////////////////////////////////////////////////////            高振凯的         /////////////////////////////////////////////////////////////

@Controller
public class PayController {

    @Autowired
    private Service_gzk serviceGzk;

    /**
     * 分页查询
     */

    @RequestMapping("/paypage")
    @ResponseBody
    public String gzkfenye(@RequestParam("currentPage") Integer currentPage,
                           @RequestParam("pageSize") Integer pageSize) {

        PageHelper.startPage(currentPage, pageSize);
        Page<Paysalary> paysalaries = serviceGzk.selAllPay();
        PageInfo<Paysalary> pageInfo = new PageInfo<Paysalary>(paysalaries);
        System.out.println("pageInfo = " + pageInfo);
        System.out.println(pageInfo.getTotal());
        return JSONObject.toJSONStringWithDateFormat(pageInfo,"yyyy-MM-dd");

    }
    //添加功能


    @RequestMapping("/addpers")
    public String add() {
        return "addpers";
    }

    @RequestMapping("/addper")
    @ResponseBody
    public boolean addper(
            @RequestParam Integer paySalaryId,
            @RequestParam Integer payNumber,
            @RequestParam Integer headcount,
            @RequestParam Integer totalBasic,
            @RequestParam Integer realSalary,
            @RequestParam String payTime) {
        boolean addper = serviceGzk.addper( paySalaryId,payNumber, headcount, totalBasic, realSalary, payTime);
        if (addper) {
            return true;
        }
        return false;
    }

    //根据ID查找pay 数据单条渲染
    @GetMapping("/selOne")
    @ResponseBody
    public String selOne(@RequestParam Integer paySalaryId) {
        List<Paysalary> list = serviceGzk.selOne(paySalaryId);
      return JSONObject.toJSONStringWithDateFormat(list,"yyyy-MM-dd");
    }

    //根据ID查找  Detailsalary表！！！！
    @GetMapping("/selDeta")
    @ResponseBody
    public String selDeta(@RequestParam Integer paySalaryId) {
        System.out.println("paySalaryId  " + paySalaryId);
        List<Detailsalary> detailsalaries = serviceGzk.selDeta(paySalaryId);
        return JSON.toJSONString(detailsalaries);
    }

    @GetMapping("/DetaOne")
    @ResponseBody
    public String DetaOne( HttpSession session) {
        Integer id = (Integer)session.getAttribute("id");
        List<Detailsalary> Detass = serviceGzk.DetaOne(id);
        return JSON.toJSONString(Detass);
    }


    ////////////////////////////////////////////////////////Detailsalary 表 查询全部     ///////////////////////////////////////////////////////
    @PostMapping("/DetaAll")
    @ResponseBody
    public String DetaAll(@RequestParam Integer paySalaryId) {
        List<Detailsalary> DetailsalaryList = serviceGzk.selDeta(paySalaryId);
        String str = JSONObject.toJSONString(DetailsalaryList);
        return str;
    }

    /////////////////////////////////////////////////////// Personsalary 表修改      ///////////////////////////////////////////////////////


    @RequestMapping("/selpers/{id}")
    public String selpers1(@PathVariable Integer id, HttpSession session) {
        session.setAttribute("id",id);
        return "薪酬发放更改";
    }

    @ResponseBody
    @RequestMapping("/selpershuidiao")
    public String selpershuidiao( HttpSession session) {

        Integer id = (Integer)session.getAttribute("id");
        List<Personsalary> personsalaries = serviceGzk.selpers(id);
        return JSON.toJSONString(personsalaries);
    }


    @GetMapping("/selper")
    @ResponseBody
    public String selpers(@RequestParam("personnelId") Integer personnelId) {
        System.out.println("personnelId  " + personnelId);
        List<Personsalary> personsalaries = serviceGzk.selpers(personnelId);

        personsalaries.forEach(System.out::println);
        return JSON.toJSONString(personsalaries);
    }


//    @ResponseBody
//    public Integer update(@ModelAttribute Contact contact) {//把传递过来的数据，直接存入实体类
//        //受影响的行数，int类型
//        int i = cd.updateById(contact);
//        return i;
//    }


    @RequestMapping("/updper")
    @ResponseBody
    public boolean updStu(
            @RequestParam Integer subsidiesId,
            @RequestParam String performance,
            @RequestParam String evection,
            @RequestParam String overtime,
            @RequestParam String deduct) {
        boolean updper = serviceGzk.updper(subsidiesId, performance, evection, overtime, deduct);

        return updper;
    }

    @RequestMapping("perindex")
    public String goPay() {
        return "薪资发放";
    }


}
